﻿// mod_motion.js

var mod_motion_timer = new Timer();
var mod_motion_img = new Array();

function mod_motion_init() {
	if( preferences.motion_url.value[preferences.motion_url.value.length - 1] != "/" ) preferences.motion_url.value = preferences.motion_url.value + "/";

	print( "** Init Widget **" );

	mod_motion_timer.interval     = 120;
	mod_motion_timer.ticking      = true;
	mod_motion_timer.onTimerFired = mod_motion_update;

	mod_motion_img[0] = new Image();
	mod_motion_img[0].window = main;
	mod_motion_img[0].src = "Resources/Grey.png"
	mod_motion_img[0].vOffset = 0;
	mod_motion_img[0].hOffset = 0;
	mod_motion_img[0].visible = true;
	mod_motion_img[0].onMultiClick = mod_motion_change;
	mod_motion_img[0].zOrder = 0;

	mod_motion_update();
	main.visible = true;
	return true;
}

function mod_motion_update() {
	print( "** Get an Update **");
	mod_motion_timer.interval = 120;

	var mod_motion_url = new URL()
	mod_motion_url.location = preferences.motion_url.value;
	mod_motion_url.timeout = 2;
	mod_motion_url.fetchAsync(mod_motion_threadsdone);

	return true;
}

function mod_motion_threadsdone( url ) {
	print( "** Got Update: " + url.response  + " **");
	if( url.response == 200 ) {
		var threadcount = url.result.match( /Running \[(.*)\] Threads/ );
		print( "** Found Threads: " + threadcount[1]  + " **");
		if(mod_motion_img.length < threadcount[1]){
			for(var i = (mod_motion_img.length - 1);i < threadcount[1]; i++) {
				mod_motion_img[i] = new Image();
				mod_motion_img[i].window = main;
				mod_motion_img[i].src = "Resources/Grey.png"
				mod_motion_img[i].vOffset = i * 32;
				mod_motion_img[i].hOffset = 0;
				mod_motion_img[i].visible = true;
				mod_motion_img[i].onMultiClick = mod_motion_change;
				mod_motion_img[i].zOrder = i;
			}
		}
			
		for(var i = 0;i < threadcount[1]; i++) {
			print( "** Requesting Thread: " + i + " **");
			var mod_motion_url = new URL();
			mod_motion_url.location = preferences.motion_url.value + i + "/detection/status";
			mod_motion_url.timeout = 5;
			//mod_motion_img[i].colorize = "#FF7F00";
			mod_motion_url.fetchAsync(mod_motion_urldone);
		}

	} else {
		print( "* Error getting Update *");

		for(var i = 0;i < mod_motion_img.length; i++) {
			mod_motion_img[i].colorize = "#FF7F00";
		}

		mod_motion_timer.interval = 1;
	}

}

function mod_motion_urldone( url ) {
	print( "** Got Thread: " + url.response );
	if( url.response == 200 ) {
		var threadid = url.result.match( /Thread (.+) Detection status/ );
		print( "-- Thread ID: " + threadid[1] );

		var threadstatus = url.result.match( /Detection status (.+)/ );
		print( "-- Thread Status: " + threadstatus[1] );

		if(threadstatus[1]=="PAUSE") mod_motion_img[threadid[1]].colorize = "#FF0000";
		else if(threadstatus[1]=="ACTIVE") mod_motion_img[threadid[1]].colorize = "#00FF00";
		else mod_motion_img[threadid[1]].colorize = "";
	} else {
		print( "** Error getting Thread **");

		for(var i = 0;i < mod_motion_img.length; i++) {
			mod_motion_img[i].colorize = "#FF7F00";
		}

		mod_motion_timer.interval = 1;
	}
	return true;
}

function mod_motion_change() {
	var mod_motion_url = new URL();
	if(this.colorize == "#00FF00") {
		print( "** Pause Thread: " + this.zOrder );
		mod_motion_url.location = preferences.motion_url.value + this.zOrder + "/detection/pause";
		mod_motion_url.fetchAsync(mod_motion_changedone);
	} else 	if(this.colorize == "#FF0000") {
		print( "** Start Thread: " + this.zOrder );
		mod_motion_url.location = preferences.motion_url.value + this.zOrder + "/detection/start";
		mod_motion_url.fetchAsync(mod_motion_changedone);
	} else {
		print( "*** Switching Status of Thread: " + this.zOrder + " ... Unknown Status of thread!!!");
	}
	return true;
}

function mod_motion_changedone( url ) {
	print( "** Thread switched **");
	mod_motion_update();
	return true;
}
